﻿using System;
using System.IO;
using System.Reflection;

namespace HashFoo.MigratorFooTests
{
    public static class Util
    {
        /// <summary>
        /// Gets the code base file as a local operating system specific path.
        /// </summary>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public static string GetCodeBaseAsLocalPath(this Assembly assembly)
        {
            var uri = new Uri(assembly.CodeBase);
            return uri.LocalPath;
        }

        /// <summary>
        /// Gets the local directory of the code base.
        /// </summary>
        /// <param name="assembly"></param>
        /// <returns></returns>
        public static string GetCodeBaseDir(this Assembly assembly)
        {
            return Path.GetDirectoryName(assembly.GetCodeBaseAsLocalPath());
        }

        /// <summary>
        /// Gets the callers executing directory (by using GetCallingAssembly).  Returns the local path string.
        /// </summary>
        /// <returns>The local path string</returns>
        public static string GetExecutingDirectory()
        {
            return new Uri(Path.GetDirectoryName(Assembly.GetCallingAssembly().CodeBase) ?? ".\\").LocalPath;
        }

        /// <summary>
        /// Builds a path relative to the callers current directory.
        /// </summary>
        /// <param name="paths">The path parts to append to the callers current directory</param>
        /// <returns>The built path</returns>
        /// <seealso cref="GetExecutingDirectory"/>
        public static string BuildPathRelativeToExecuting(params string[] paths)
        {
            return Path.Combine(GetExecutingDirectory(), Path.Combine(paths));
        } 
    }
}